﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Collections.Generic;

/// <summary>
/// Summary description for ExcuteSQL
/// </summary>
public static class ExcuteSQL
{
    static SqlConnection con;
    static SqlDataAdapter dap;
    static SqlCommand cmd;
    static VietStar.Library.Commom.SqlServer.Connection cls = new VietStar.Library.Commom.SqlServer.Connection();
    public static DataTable Store(string sql, List<SqlParameter> param, CommandType type)
    {
        DataTable dt = new DataTable();
        try
        {
            con = cls.ReadFromWebConfig();
            dap = new SqlDataAdapter();
            cmd = new SqlCommand();
            cmd.Transaction = con.BeginTransaction();
            cmd.Connection = con;
            cmd.CommandText = sql;
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);
            }
            cmd.CommandType = type;
            dap.SelectCommand = cmd;
            dap.Fill(dt);
            cmd.Transaction.Commit();
            con.Close();
        }
        catch (Exception ex)
        { }
        return dt;
    }
    public static bool Query(string sql, List<SqlParameter> param, CommandType type)
    {
        bool flag = false;
        try
        {
            con = cls.ReadFromWebConfig();
            cmd = new SqlCommand(sql, con);
            cmd.CommandType = type;
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);
            }            
            cmd.ExecuteNonQuery();
            flag = true;
            con.Close();
        }
        catch (Exception ex)
        { }
        return flag;
    }
}
